package com.mdinfo.gw.door.mapper

import com.baomidou.mybatisplus.mapper.BaseMapper
import com.mdinfo.gw.door.bean.DoorCard
import com.mdinfo.gw.door.vo.DoorCardListUnBindEmpVo
import org.apache.ibatis.annotations.Param
import org.apache.ibatis.annotations.Select

/**
 * Created by Administrator on 2018/1/9.
 */
interface DoorCardMapper : BaseMapper<DoorCard> {

    /**根据设备号查询*/
    @Select("""<script>
        select * from door_card t where t.devId = #{devId}
    </script>""")
    fun getByDevId(devId:String):DoorCard?

    /**列表查询*/
    @Select("""<script>
        select * from door_card t where 1=1
        <if test="key!=null">
            and t.devId like CONCAT("%",#{key},"%")
        </if>
        <if test="deptId!=null">
            and t.deptId = #{deptId}
        </if>
        <if test="deptIdList!=null and deptIdList.size>0">
            and t.deptId in
            <foreach collection="deptIdList" item="dept_id" open="(" close=")" separator=",">
                #{dept_id}
            </foreach>
        </if>
        order by t.createTime desc
        limit #{start},#{num}
    </script>""")
    fun listQuery(@Param("start")start:Int,
                  @Param("num")num:Int,
                  @Param("key")key:String?,
                  @Param("deptId")deptId:Int?,
                  @Param("deptIdList")deptIdList:List<Int>)
            :List<DoorCard>

    /**列表查询总数*/
    @Select("""<script>
        select count(1) from door_card t where 1=1
        <if test="key!=null">
            and t.devId like CONCAT("%",#{key},"%")
        </if>
        <if test="deptId!=null">
            and t.deptId = #{deptId}
        </if>
        <if test="deptIdList!=null and deptIdList.size>0">
            and t.deptId in
            <foreach collection="deptIdList" item="dept_id" open="(" close=")" separator=",">
                #{dept_id}
            </foreach>
        </if>
    </script>""")
    fun countListQuery(@Param("key")key:String?,
                       @Param("deptId")deptId:Int?,
                       @Param("deptIdList")deptIdList:List<Int>)
            :Int


    /**查询本部门没有绑定门卡的人员*/
    @Select("""<script>
        select e.id empId, e.name empName from sys_emp e
        where e.deptId = #{deptId}
        and e.id not in (
            select t.empId from door_card t
            where t.empId is not null
            and t.empId > 0
            and t.deptId = #{deptId}
        )
    </script>""")
    fun listUnBindEmp(@Param("deptId")deptId:Int)
            :List<DoorCardListUnBindEmpVo>

    /**查询部门下所有的门卡*/
    @Select("""<script>
        select * from door_card t where t.deptId = #{deptId}
    </script>""")
    fun listAllByDept(@Param("deptId")deptId:Int):List<DoorCard>




    /**根据开放时间id查询绑定的门卡*/
    @Select("""<script>
        select c.* from door_card c
        join door_card_time_bind b
        on (c.id = b.cardId and b.timeId = #{timeId})
        order by c.createTime desc limit #{start},#{num}
    </script>""")
    fun listByTimeId(@Param("timeId")timeId:Int,
                     @Param("start")start:Int,
                     @Param("num")num:Int)
            :List<DoorCard>

    /**根绝开放时间id查询绑定的门卡总数*/
    @Select("""<script>
        select count(1) from door_card c
        join door_card_time_bind b
        on (c.id = b.cardId and b.timeId = #{timeId})
    </script>""")
    fun countListByTimeId(@Param("timeId")timeId:Int):Int
}